﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using BusinessLogicLayer;

namespace ChamCong.Admin
{
    public partial class QuanLyPhanQuyen : BasePage
    {
        QuanLyPhanQuyenBLL q = new QuanLyPhanQuyenBLL();
        NhanVienBLL nvBLL = new NhanVienBLL();
        PhongBanBLL pb = new PhongBanBLL();
        CT_DangNhapBLL tk = new CT_DangNhapBLL();
        public static int nv;
        TreeViewState stateTreeviewUser;
        protected void Page_Load(object sender, EventArgs e)
        {
            stateTreeviewUser = new TreeViewState("TreeViewUserState");
            nv = Convert.ToInt32(Request.QueryString["nv"]);
            string tt = Request.QueryString["tt"];
            if (!IsPostBack)
            {
                if (tt == "qlr")
                {
                    upQLR.Visible = true;
                    pnlQLR.Visible = true;
                    pnlPQ.Visible = false;
                    upPQ.Visible = false;
                }
                else
                {
                    upPQ.Visible = true;
                    pnlPQ.Visible = true;
                    HienThiTreeViewNV();
                    bindRoleNV();
                }
                
            }
        }
        public void bindRoleNV()
        {
            if (nv > 0)
            {
                pnlThongTinNV.Visible = true;
                lblPhongBan.Text = nvBLL.LayNhanVien(nv).PHONGBAN.TenPB;
                lblHoTenNV.Text = nvBLL.LayNhanVien(nv).HoTen;
                cblDanhSachRole.DataSource = q.layTatCaRole();
                cblDanhSachRole.DataTextField = "TenRole";
                cblDanhSachRole.DataValueField = "MaRole";
                cblDanhSachRole.DataBind();
                btnCapNhat.Visible = true;
                List<int> lstRoleNV = q.LayMaRoleTuNV(nv);
                for (int i = 0; i < cblDanhSachRole.Items.Count; i++)
                {
                    for (int j = 0; j < lstRoleNV.Count; j++)
                    {
                        if (cblDanhSachRole.Items[i].Value == lstRoleNV[j].ToString())
                        {
                            cblDanhSachRole.Items[i].Selected = true;
                        }
                    }
                }                 
            }
           
        }
        public void HienThiTreeViewNV()
        {
            List<PHONGBAN> Cap1 = pb.LayTatCa().ToList();

            foreach (var item in Cap1)
            {
                TreeNode child = new TreeNode(item.TenPB + " (" + item.MaPB + ")", item.MaPB.ToString());
                //child.NavigateUrl = "QuanLyPhanQuyen.aspx?nv=" + item.STT;
                child.ToolTip = item.TenPB.ToString();
                tvwUsers.Nodes.Add(child);
                List<NHANVIEN> Cap2 = tk.layDSNhanVienTheoPB((int)item.MaPB);
                foreach (var item2 in Cap2)
                {
                    TreeNode child2 = new TreeNode(item2.HoTen + " (" + item2.MaNV + ")", item2.MaNV.ToString());
                    child2.Value = "QuanLyPhanQuyen.aspx?tt=pq&nv=" + item2.MaNV;
                    child2.ToolTip = item2.HoTen.ToString();
                    child.ChildNodes.Add(child2);
                }
            }
            tvwUsers.CollapseAll();
            if (stateTreeviewUser.IsSaved)
            {
                stateTreeviewUser.RestoreTreeView(tvwUsers, "TreeViewUserState", HttpContext.Current);
            }
        }

        protected void btnThemRole_Click(object sender, EventArgs e)
        {
            if (q.KiemTraTonTai(tbxTenRole.Text) == null)
            {
                ROLE r = new ROLE();
                r.TenRole = tbxTenRole.Text;
                r.Xoa = 1;
                q.themRole(r);
                dvDanhSachRole.DataBind();
            }
            else
            {
                sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Error, "Thông báo", q.KiemTraTonTai(tbxTenRole.Text) + "đã tồn tại!");
            }
        }

        protected void dvDanhSachRole_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton imgbtXoa = (ImageButton)e.Row.FindControl("imgbtXoa");
                ImageButton imgbtSua = (ImageButton)e.Row.FindControl("imgbtSua");
                ImageButton imgbtXem = (ImageButton)e.Row.FindControl("imgbtXem");

                if (imgbtXoa != null)
                    imgbtXoa.Attributes["MaRole"] = e.Row.Cells[0].Text;
                if (imgbtSua != null)
                    imgbtSua.Attributes["MaRole"] = e.Row.Cells[0].Text;
                if (imgbtXem != null)
                    imgbtXem.Attributes["MaRole"] = e.Row.Cells[0].Text;
            }
        }

        protected void imgbtXoa_Click(object sender, ImageClickEventArgs e)
        {
            
            ImageButton btn = (ImageButton)sender;
            int _MaRole = int.Parse(btn.Attributes["MaRole"]);

            try
            {
                q.XoaRole(_MaRole);
                dvDanhSachRole.DataBind();
            }
            catch
            {
                sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Error, "Thông báo", "Không thể xóa!");
            }
        }

        protected void imgbtSua_Click(object sender, ImageClickEventArgs e)
        {
            
            ImageButton btn = (ImageButton)sender;
            int _MaRole = int.Parse(btn.Attributes["MaRole"]);

            try
            {
                if (IsPostBack)
                {
                    ROLE r = q.layCTRoleTheoID(_MaRole);
                    tbxIDRole.Enabled = false;
                    tbxIDRole.Text = r.MaRole.ToString();
                    tbxTenRole.Text = r.TenRole;
                    btnThemRole.Visible = false;
                    btnCapNhatRole.Visible = true;
                    btnQuayLai.Visible = true;
                    //ShowEditButton();
                }
            }
            catch(Exception ex)
            {
                sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Error, "Thông báo", ex.Message);
            }
        }

        protected void btnCapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                q.XoaRoleNV(nv);
                for (int i = 0; i < cblDanhSachRole.Items.Count; i++)
                {
                    CT_ROLE ctr = new CT_ROLE();
                    ctr.MaNV = nv;
                    if (cblDanhSachRole.Items[i].Selected)
                    {
                        ctr.MaRole = Convert.ToInt32(cblDanhSachRole.Items[i].Value);

                        q.themTaiKhoanVaoRole(ctr);
                    }
                }
                sbcMsgBox2.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Success, "Thông báo", "Cập nhật thành công!");
            }
            catch (Exception ex)
            {
                sbcMsgBox2.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Error, "Thông báo", ex.Message);
            }

        }

        protected void btnCapNhatRole_Click(object sender, EventArgs e)
        {
            ROLE r = new ROLE();
            r.MaRole = int.Parse(tbxIDRole.Text);
            r.TenRole = tbxTenRole.Text;

            if (q.KiemTraTonTai(r.TenRole) == null)
            {
                q.suaRole(r);
                dvDanhSachRole.DataBind();
                sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Success, "Thông báo", "Cập nhật thành công!");
            }
            else
            {
                sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Error, "Thông báo", "Tên role đã được sử dụng!");
            }
        }

        protected void btnQuayLai_Click(object sender, EventArgs e)
        {
            Page.Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
        }

        protected void tvwUsers_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (tvwUsers.SelectedNode.Depth == 1)
            {
                stateTreeviewUser.SaveTreeView(tvwUsers, "TreeViewUserState", HttpContext.Current);
                Response.Redirect(tvwUsers.SelectedNode.Value);
            }
        }


    }
}